// packageA/pages/all-order/index.js
const WXAPI = require("../../../api/api");
const baseUrl = require("../../../api/config").dev; //这里用的是ES6的写法
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgBaseUrl: `${baseUrl["imgBaseUrl"]}`,
    tabList1:[0,1,3,4,9],
    tabList2:[0,3,4,5,9],
    isLoading: false,
    serviceNavList: [
      {
        title: "全部",
        status: 0,
        iconName: "my-all",
      },
      {
        title: "待服务",
        status: 3,
        iconName: "my-waitservice",
      },
      {
        title: "待完成",
        status: 4,
        iconName: "my-waitover",
      },
      {
        title: "已完成",
        status: 5,
        iconName: "my-over",
      },
      {
        title: "已取消",
        status: 9,
        iconName: "my-cancel",
      },
    ],
    goodsNavList: [
      {
        title: "全部",
        status: 0,
        iconName: "my-all",
      },
      {
        title: "待付款",
        status: 1,
        iconName: "my-waitpay",
      },
      {
        title: "待收货",
        status: 3,
        iconName: "my-waitreceive",
      },
      {
        title: "已完成",
        status: 4,
        iconName: "my-over",
      },
      {
        title: "已取消",
        status: 9,
        iconName: "my-cancel",
      },
    ],
    serviceList: [],
    goodsList: [],
    currentSelectOrderType: "2",
    currentOrderStatus: "",
    pageSize: 15,
    pageNum: 1,
    currentTab: 0,
    top: 0,
    total: 0,
    triggered: false,
    currentPageNum: 0,
    listIsOver: false,
  },
  // 点击状态按钮切换
  switchTab(event) {
    console.log('切换了');
    let index = event.detail.current;
    this.setData({
      pageNum: 1,
      serviceList: [],
      goodsList: [],
    });
    let orderType = this.data.currentSelectOrderType;
    if (orderType == "1") {
      this.getOrderList(orderType, this.data.goodsNavList[index].status);
      this.setData({
        currentTab: index,
        currentOrderStatus: this.data.goodsNavList[index].status,
      });
    }
    if (orderType == "2") {
      this.getOrderList(orderType, this.data.serviceNavList[index].status);
      this.setData({
        currentTab: index,
        currentOrderStatus: this.data.serviceNavList[index].status,
      });
    }
  },
  selectOrderType(val) {
    this.setData({
      currentSelectOrderType: val.detail.code,
      show1: this.data.currentSelectOrderType == 1,
      show2: this.data.currentSelectOrderType == 2,
      currentTab: 0,
      currentOrderStatus: 0,
    });
    this.getOrderList(
      this.data.currentSelectOrderType,
      this.data.currentOrderStatus
    );
  },
  changeOrderStatus(event) {
    console.log('切换订单状态');
    let status = event.currentTarget.dataset.status;
    let index = event.currentTarget.dataset.index;
    this.setData({
      currentTab: index,
      currentOrderStatus: status,
    });
  },
  //上拉触底
  onScrollBottom() {
    if (this.data.pageNum < this.data.currentPageNum) {
      this.setData({
        pageNum: this.data.pageNum + 1,
      });
      this.getOrderList(
        this.data.currentSelectOrderType,
        this.data.currentOrderStatus
      );
    } else if (
      this.data.pageNum > this.data.currentPageNum ||
      this.data.pageNum == this.data.currentPageNum
    ) {
      this.setData({
        listIsOver: true,
      });
    }
  },
  //下拉刷新
  async onRefresh() {
    this.setData({
      pageNum: 1,
      serviceList: [],
      goodsList: [],
      listIsOver: false,
    });
    this.getOrderList(
      this.data.currentSelectOrderType,
      this.data.currentOrderStatus
    );
  },
  async getOrderList(type, status, isLoading) {
    this.setData({
      listIsOver:false,
      isLoading: true,
    });

    let data = {
      iStatus: status == 0 ? "" : status || "",
      PageIndex: this.data.pageNum,
      PageSize: this.data.pageSize,
    };
    if (type == 1) {
      // 商品订单
      let isCustomLoading = true;
      let listArr = [];
      let res = await WXAPI.getGoodsOrder(data, isCustomLoading);
      if (res.data.code == 0) {
        if (this.data.pageNum == 1) {
          listArr = res.data.data.list;
        } else {
          listArr = this.data.goodsList;
          Array.prototype.push.apply(listArr, res.data.data.list);
        }
        this.setData({
          listIsOver: true,
        });
        this.setData({
          total: res.data.data.total,
          goodsList: listArr,
          triggered: false,
          currentPageNum: res.data.data.list.length,
          isLoading: false,
        });
      }
    }
    if (type == 2) {
      // 服务订单
      let isCustomLoading = true;
      let listArr = [];
      let res = await WXAPI.getServiceOrder(data, isCustomLoading);
      if (res.data.code == 0) {
        if (this.data.pageNum == 1) {
          listArr = res.data.data.list;
        } else {
          listArr = this.data.serviceList;
          Array.prototype.push.apply(listArr, res.data.data.list);
        }
        this.setData({
          listIsOver: true,
        });
        this.setData({
          total: res.data.data.total,
          serviceList: listArr,
          triggered: false,
          currentPageNum: res.data.data.list.length,
          isLoading: false,
        });
      }
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options) {
      if (options.orderType) {
        this.setData({
          currentSelectOrderType: options.orderType,
        });
      }
      if (options.navigationBarTitle) {
        wx.setNavigationBarTitle({
          title: options.navigationBarTitle,
        });
      }
      if (options.status) {
        let currentTab = 0
        if(this.data.currentSelectOrderType == 1){
         currentTab =  this.data.tabList1.findIndex(item=>item == options.status)
        }
        if(this.data.currentSelectOrderType == 2){
          currentTab =  this.data.tabList2.findIndex(item=>item == options.status)
         }
        this.setData({
          currentOrderStatus: options.status,
          currentTab
        });
      }
      
      this.getOrderList(options.orderType, options.status, true);
    } else {
      this.getOrderList(
        this.data.currentSelectOrderType,
        this.data.currentOrderStatus,
        true
      );
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // this.getOrderList(this.data.currentSelectOrderType, this.data.currentOrderStatus)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
